package cn.sxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sxt.userscenter.common.controller.BaseController;
import cn.sxt.userscenter.common.util.ConstatFinalUtil;
import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersEnum;
import cn.sxt.userscenter.users.service.IUsersService;

/**
 * 不需要登录的操作
 * @author zhong
 *
 */
@Controller
public class NoLoginController extends BaseController{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 管理员列表页面
	 * @return
	 */
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("---- adminsList ----");

		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		/* 按照id查询 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		/* 存储结果 */
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/adminsList" ; 
	}
	
	/**
	 * 打开登录页面
	 * @return
	 */
	@RequestMapping("/login")
	public String login(){
		ConstatFinalUtil.LOGGER.info("-------login------");
		return "/head/login";
	}
	
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpSession session, String id)
	{
		ConstatFinalUtil.LOGGER.info("---- loginSubmit ----");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		
		/* 从session中取验证码 */
		String sessCode = session.getAttribute("rand") + "";
		
		/* 验证码比对完成,从session中移除 */
		session.removeAttribute("rand");
		if(sessCode != null && sessCode.equalsIgnoreCase(code))
		{
			/* 根据用户名查询用户 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				/* 邮箱存在 */
				if(this.encryptUtil.decode(password, users.getPassword()))
				{
					if(users.getStatus() == AUsersEnum.STATUS_ENABLE.getStatus())
					{
						/* 登陆成功 */
						/* 放入session */
						session.setAttribute("users", users);
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						
						users.setLastLoginTime(new Date());
						this.usersService.updateOneUsersService(users);
						
						/* 客户端跳转到登陆成功的页面 */
						return "redirect:/head/users/main.htm" ;
					}else
					{
						/* 账户被禁用 */
						this.info = "账户被禁用" ; 
					}
				}else
				{
					/* 密码不正确 */
					this.info = "密码不正确" ; 
				}
				request.setAttribute("email", email);
			}else
			{
				/* 邮箱不存在 */
				this.info = "邮箱不存在" ; 
			}
		}else
		{
			/* 验证码不正确 */
			this.info = "验证码不正确" ; 
		}
		
		request.setAttribute("info", info);
		
		/* 模拟登陆成功 */
		/*if("1".equalsIgnoreCase(id))
		{
			 登陆成功 
			 客户端跳转到登陆成功的页面 
			return "redirect:/head/users/main.htm" ;
		}*/
		return this.login() ; 
	}
}



